package com.sinochem.sales.auth.config;

import com.alibaba.fastjson.JSON;
import com.sinochem.sales.admin.api.to.UserTO;
import com.sinochem.sales.common.constant.SecurityConstants;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;

import java.util.Map;

/**
 * @author xuan😴🐶🐱🐽🌺  <a href="zhangshixuanj@163.com">联系作者</a>
 * @version Revision:v1.0,Date: 2018/4/1 16:59
 * @project parent
 * @description 自定义token信息中添加的信息
 * @Modification Date: 2018/4/1 16:59  {填写修改说明}
 */
public class CustomJwtAccessTokenConverter extends JwtAccessTokenConverter {

    @Override
    public Map<String, ?> convertAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication) {
        Map<String, Object> representation = (Map<String, Object>) super.convertAccessToken(token, authentication);
        UserTO userTO = JSON.parseObject(JSON.toJSONString(authentication.getPrincipal()), UserTO.class);
        representation.put("userId", userTO.getUserId());
        representation.put("username", userTO.getUsername());
        return representation;
    }

    @Override
    public OAuth2AccessToken extractAccessToken(String value, Map<String, ?> map) {
        return super.extractAccessToken(value, map);
    }

    @Override
    public OAuth2Authentication extractAuthentication(Map<String, ?> map) {
        return super.extractAuthentication(map);
    }
}
